package com.example.doer.reading.global;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.ab.global.AbAppConfig;
import com.example.doer.reading.AppDataModel.AppUser;
import com.example.doer.reading.MyReadTool.Config;
import com.example.doer.reading.MyReadTool.util.PageFactory;

import org.litepal.LitePalApplication;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by doer on 2017/7/14.
 */

public class MyApplication extends LitePalApplication {
    public static AppUser user;
    public SharedPreferences sharedPreferences = null;
    public static volatile Context applicationContext = null;
    private String mSampleDirPath;
    public static final String SAMPLE_DIR_NAME = "baiduTTS";
    public static final String SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female.dat";
    public static final String SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male.dat";
    public static final String TEXT_MODEL_NAME = "bd_etts_text.dat";
    public static final String LICENSE_FILE_NAME = "temp_license";
    public static final String ENGLISH_SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female_en.dat";
    public static final String ENGLISH_SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male_en.dat";
    public static final String ENGLISH_TEXT_MODEL_NAME = "bd_etts_text_en.dat";

    private static final int PRINT = 0;
    private static final int UI_CHANGE_INPUT_TEXT_SELECTION = 1;
    private static final int UI_CHANGE_SYNTHES_TEXT_SELECTION = 2;

    @Override
    public void onCreate(){
        super.onCreate();
        Config.createConfig(this);
        PageFactory.createPageFactory(this);
        LitePalApplication.initialize(this);
        applicationContext = getApplicationContext();
        sharedPreferences=getSharedPreferences(AbAppConfig.SHARED_PATH, Context.MODE_PRIVATE);
        initLoginParams();
        initialEnv();
    }

    private void initLoginParams(){
        String userID = sharedPreferences.getString(Constant.USERIDCOOKIE,null);
        String userPwd = sharedPreferences.getString(Constant.USERPASSWORDCOOKIE,null);
        boolean islogin=sharedPreferences.getBoolean(Constant.ISLOGINUSER,false);
        user=new AppUser();
        user.setUserID(userID);
        user.setUserPassWord(userPwd);
        user.setLoginUser(islogin);
    }

    public void updateLoginParams(AppUser appUser){
        user=appUser;
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(Constant.USERIDCOOKIE,user.getUserID());
        editor.putString(Constant.USERPASSWORDCOOKIE,user.getUserPassWord());
        editor.putBoolean(Constant.ISLOGINUSER,user.isLoginUser());
        editor.commit();
    }

    public boolean IfFirstStart(){
        return sharedPreferences.getBoolean(Constant.ISFIRSTSTART,true);
    }
    public void setFirstStart(boolean ifstart){
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean(Constant.ISFIRSTSTART,ifstart);
        editor.commit();
    }

    public void clearLoginParams() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.putBoolean(Constant.ISFIRSTSTART,false);
        editor.commit();
        user = null;
    }

    @Override
    public void onTerminate(){super.onTerminate();}
    public String getTTPath(){
        return mSampleDirPath;
    }
    private void initialEnv() {
        if (mSampleDirPath == null) {
            String sdcardPath = Environment.getExternalStorageDirectory().toString();
            mSampleDirPath = sdcardPath + "/" + SAMPLE_DIR_NAME;
        }
        makeDir(mSampleDirPath);
        copyFromAssetsToSdcard(false, SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME);
        copyFromAssetsToSdcard(false, SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_MALE_MODEL_NAME);
        copyFromAssetsToSdcard(false, TEXT_MODEL_NAME, mSampleDirPath + "/" + TEXT_MODEL_NAME);
//        copyFromAssetsToSdcard(false, LICENSE_FILE_NAME, mSampleDirPath + "/" + LICENSE_FILE_NAME);
        copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/"
                + ENGLISH_SPEECH_FEMALE_MODEL_NAME);
        copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/"
                + ENGLISH_SPEECH_MALE_MODEL_NAME);
        copyFromAssetsToSdcard(false, "english/" + ENGLISH_TEXT_MODEL_NAME, mSampleDirPath + "/"
                + ENGLISH_TEXT_MODEL_NAME);
    }

    private void makeDir(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    private void copyFromAssetsToSdcard(boolean isCover, String source, String dest) {
        File file = new File(dest);
        if (isCover || (!isCover && !file.exists())) {
            InputStream is = null;
            FileOutputStream fos = null;
            try {
                is = getResources().getAssets().open(source);
                String path = dest;
                fos = new FileOutputStream(path);
                byte[] buffer = new byte[1024];
                int size = 0;
                while ((size = is.read(buffer, 0, 1024)) >= 0) {
                    fos.write(buffer, 0, size);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
